package com.fzb.forum4j.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;


import com.fzb.forum4j.dao.api.IUserDao;
import com.fzb.forum4j.po.QueryMode;
import com.fzb.forum4j.po.User;
@Repository("userDaoImpl")
public class UserDaoImpl extends HibBaseImpl<User> implements IUserDao {

	@Override
	public void login(QueryMode<User> t) {
		t.setT((User) getSession().createQuery("from User u where u.userName=:userName and u.password=:password").setProperties(t.getT()).uniqueResult());
	}
	@Override
	public void getList(QueryMode<User> t) {
		String hqlWhere = "";
		if(t.getT().getUserName()!=null){
			hqlWhere="where t.user.userName like '"+t.getT().getUserName()+"'";
			
		}
		 CommonsQuery(t, "select count(t.userId) from User t "+ hqlWhere,
				 "select new map(t.userId as id,t.userName as userName,t.password as password,t.regTime as regTime) from User t "+hqlWhere+" order by ", "t", "t.userId");
	}
	@Override
	public Boolean existsUserNameAndEmail(User user) {
		if(getSession().createQuery("from User u where u.userEmail=:userEmail and u.userName=:userName").setProperties(user).uniqueResult()!=null){
			return true;
		}
		return false;
	}
	@Override
	public User loadUserByUserName(String userName) {
		return (User) getSession().createQuery("select t from User t where t.userName=:userName").setParameter("userName", userName).uniqueResult();
		
	}
	@Override
	public User getSimpleDataByUserName(String userName) {
		return (User) getSession().createQuery("select new com.fzb.forum4j.po.User(t.userName,t.regTime,t.details.headerFileName as headerFileName) from User t where t.userName=:userName").setParameter("userName", userName).uniqueResult();
	}
	@Override
	public User getUserNameByNickName(String nickName) {
		return (User) getSession().createQuery("select new com.fzb.forum4j.po.User(t.userName,t.userId) from User t where t.nickName=:nickName").setParameter("nickName", nickName).uniqueResult();
	}
	@Override
	public void getMessageByUserId(QueryMode<User> qmd) {
		Long l=(Long) getSession().createQuery("select count(m) from User t left join t.sendMessages m where m.receiveUser="+qmd.getT().getUserId()+"").uniqueResult();
		if(l!=null){
			Map<String, Object> message=new HashMap<String, Object>();
			message.put("total", l);
			List<Map<String,String>> rows=getSession().createQuery("select new map(m.sendUser.userName as sendUserName,m.sendUser.nickName as nickName,m.link as link,m.sendTime as sendTime) from User t left join t.sendMessages m where m.receiveUser="+qmd.getT().getUserId()+"").list();
			message.put("rows", rows);
			qmd.getData().put("messages", message);
		}
		/*QueryDataByQmd(qmd, "t as rm", "1=1", "sendMessages.mrid");
		System.out.println(qmd.getData());*/
		
	}
	@Override
	public List<Map<String,String>> getHotUser() {
		return getSession().createQuery("select new map(t.nickName as nickName,t.userName as userName,d.headerFileName as header) from User t left join t.details d order by t.details.icon").setFirstResult(0).setMaxResults(20).list();
	}
}
 